<template>
    <div>
        <!--表单查询-->
        <el-form :inline="true" :model="formInline" class="demo-form-inline">
            <el-form-item label="工作包号">
                <el-input v-model="formInline.workpageCode"  size="mini" placeholder="工作包号"></el-input>
            </el-form-item>
            <el-form-item label="AO号">
                <el-input v-model="formInline.aoCode"  size="mini" placeholder="AO号"></el-input>
            </el-form-item>
            <el-form-item label="站位">
                <el-input v-model="formInline.station"  size="mini" placeholder="站位"></el-input>
            </el-form-item>
            <el-form-item label="架次">
                <el-input v-model="formInline.planeNo"  size="mini" placeholder="架次"></el-input>
            </el-form-item>
            <el-form-item label="托盘">
                <el-input v-model="formInline.palletCode"  size="mini" placeholder="托盘"></el-input>
            </el-form-item>
            <el-form-item>
                <el-button type="primary"  size="mini" @click="doQuery">查询</el-button>
                <el-button type="primary" size="mini" @click="doReset">重置</el-button>
                <el-button type="primary" size="mini" @click="exeTask">执行任务</el-button>
                <el-button type="primary" size="mini" @click="taskGoOn">缓存箱任务下发</el-button>
                <el-button type="primary" size="mini"  @click="lookupHcx">查看缓存箱</el-button>
                <el-button type="primary" size="mini"  @click="showReplaceToolView">更换托盘</el-button>
                <el-button type="primary" size="mini" @click="bindWorkbinTool">绑定托盘箱子</el-button>
                <el-button size="mini" type="primary" @click="cleanWaitingTask">清理等待任务</el-button>
                <el-button type="primary" size="mini"  @click="lookupPackageRule">查看工作包规则</el-button>
             <!--  <el-button type="primary" size="mini"  @click="packageTopMatch">工作包预配套</el-button>--> 
            </el-form-item>
        </el-form>
        <el-table :data="tableData"  height="250"  border style="width: 100%" @selection-change="handleSelectionChange">
            <el-table-column type="selection" width="55"></el-table-column>
            <el-table-column type="index" width="55"></el-table-column>
            <el-table-column prop="toolCode" label="托盘"  width="160"> </el-table-column>
            <el-table-column prop="workbinCode" label="缓存箱"  width="160"> </el-table-column>
            <el-table-column prop="aoCode" label="AO号"  width="180" show-overflow-tooltip> </el-table-column>
            <el-table-column prop="station" label="站位"  width="180" show-overflow-tooltip> </el-table-column>
            <el-table-column prop="packageCode" label="工作包号"  width="180" show-overflow-tooltip> </el-table-column>
            <el-table-column prop="itemCode" label="图号"  width="180" show-overflow-tooltip> </el-table-column>
            <el-table-column prop="mProductNumber" label="型号"  show-overflow-tooltip> </el-table-column>
            <el-table-column prop="qualityCode" label="质量编号"  show-overflow-tooltip> </el-table-column>
            <el-table-column prop="isSortName" label="分拣状态"  show-overflow-tooltip> </el-table-column>
        </el-table>
        <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page.sync="page.currentPage"
                :page-sizes="[100, 200, 300, 400]" :page-size="page.pageSize" layout="total,sizes, prev, pager, next" :total="page.total">
        </el-pagination>

        <el-drawer  title="缓存箱列表" size="75%"  :visible.sync="drawer.drawer" :with-header="false">
            <el-table :data="drawer.tableData" style="width: 100%" row-key="id" height=600  highlight-current-row  @current-change="currentTableData">
                <el-table-column  prop="workbinCode" label="缓存箱名字" width="150"> </el-table-column>
                <el-table-column prop="workbinStatus" label="状态" width="130"> </el-table-column>
                <el-table-column prop="taskType" label="需手动处理" width="130"> </el-table-column>
                <el-table-column prop="roadway" label="巷道" width="120"> </el-table-column>
                <el-table-column prop="taskCnt" label="当前巷道排队任务数" width="120"> </el-table-column>
                <el-table-column prop="waitMin" label="预计等待时长(分钟)" width="120"> </el-table-column>
                <el-table-column prop="itemCode" label="图号"></el-table-column>
                <el-table-column prop="itemQty" label="待入数量"></el-table-column>
                <el-table-column prop="aoCode" label="配套AO" show-overflow-tooltip></el-table-column>
                <el-table-column prop="mProductNumber" label="型号"></el-table-column>
                <el-table-column prop="createTime" label="创建时间" width="150" ></el-table-column>
                <el-table-column prop="orderTime" label="下发时间" width="150" ></el-table-column>
                <el-table-column prop="parentTaskId" label="移库中" width="150" ></el-table-column>
                <el-table-column label="操作">
                    <template slot-scope="scope">
                        <el-button  size="mini" v-if="scope.row.taskType=='可以创建任务'" @click="handleEdit(scope.$index, scope.row)">创建任务</el-button>
                    </template>
                </el-table-column>
            </el-table>
        </el-drawer>
        <el-drawer  title="更换托盘列表" size="75%"  :visible.sync="toolDrawer.drawer" :with-header="false">
            <el-button size="mini" type="primary" style="margin:2px 6px" @click="replaceToolCode">更换托盘</el-button>
            <el-button size="mini" type="primary" style="margin:2px 6px" @click="replaceToolByHandle">手动更换托盘</el-button>
            <el-table :data="toolDrawer.tableData" style="width: 100%" row-key="id" height=600  highlight-current-row  @selection-change="currentToolTableData">
                <el-table-column type="selection" width="50" ></el-table-column>
                <el-table-column prop="toolCode" label="托盘号" width="150" ></el-table-column>
                <el-table-column prop="palletStatus" label="托盘状态" width="180" ></el-table-column>
                <el-table-column prop="isSortName" label="是否参与分拣" width="180" ></el-table-column>
                <el-table-column prop="taskStatus" label="任务状态" width="180" ></el-table-column>
                <el-table-column prop="packageCode" label="工作包" ></el-table-column>
                <el-table-column prop="aoCode" label="AO号"  ></el-table-column>
            </el-table>
        </el-drawer>
    </div>
</template>
<script>
import request from "@/utils/request.js";
export default {
    data(){
        return {
            formInline:{workpageCode:"",aoCode:"",planeNo:"",palletCode:"",station:""},//查询表单
            drawer:{
                drawer:false,
                tableData:[],
                currentData:{}
            },
            toolDrawer:{
                drawer:false,
                tableData:[],
                currentData:[]
            },
            page:{currentPage:1,pageSize:20,total:0},//分页参数
            tableData:[],
            selectData:[]  //勾选的数据
        }
        
    },
    methods:{
        currentTableData(val){
            this.drawer.currentData = val;
        },
        currentToolTableData(val){
            this.toolDrawer.currentData = val;
        },
        handleSelectionChange(val){
            this.selectData = val;
        },
        handleSizeChange(val){
            this.page.pageSize = val;
            this.$options.methods.doQuery.bind(this)();
        },
        handleCurrentChange(val){
            this.page.pageSize = val;
            this.$options.methods.doQuery.bind(this)();
        },
        doQuery(){
            request.post("/PointPackageSortRest/queryPalletTask",{"page":this.page,"queryParam":this.formInline}).then((res)=>{
                console.log("res",res);
                this.tableData = res.data.list;
                this.page.total = res.data.total
            });
        },
        doReset(){
            Object.assign(this.formInline,this.$options.data().formInline);
        },
        exeTask(){
            if(this.selectData.length!=1){
                this.$Alert({type:"error",message:"请选择一条记录",title:"错误"});
                return ;
            }
            var toolCode = this.selectData[0].toolCode;
            request.post("/PointPackageSortRest/startPalletSortNewModel",{toolCode:toolCode}).then((res)=>{
                if(res.data.retCode=="200"){
                    this.$Alert({title: "成功", message: res.data.message,type: "success"});
                    return ;
                }else{
                    this.$Alert({title: "失败", message: res.data.message,type: "error"});
                    return ;
                }
            });
        },
        taskGoOn(){
            this.$Alert({title: "成功", message:"已点击按钮，请勿重复点击",type: "success"});

            request.post("/taskGoOn/sortGoingOn",{}).then((res)=>{
                if(res.data.retCode=="200"){
                    this.$Alert({title: "成功", message: res.data.message,type: "success"});
                    return ;
                }else{
                    this.$Alert({title: "失败", message: res.data.message,type: "error"});
                    return ;
                }
            });
        },
        lookupHcx(){
            this.drawer.drawer = true;
            this.drawer.tableData = [];
            var that = this;
            request.post("/PointPackageSortRest/searchSortingTask",{}).then((res)=>{
                console.log("res1",res);
                that.drawer.tableData = res.data;
            })
        },
        bindWorkbinTool(){
            this.$confirm('此操作执行此操作?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                request.post("/PointPackageSortRest/assginPallet",{}).then((res)=>{
                this.$Alert({message:res.data.retMsg});
            })
            }).catch(() => { });

            
        },
        handleEdit(index, row) {
            request.post("/PointPackageSortRest/createGkLog",{ID:row.id}).then((res)=>{
            console.log("res1",res);
                if(res.data.retCode=='200'){
                    this.$Alert({title: "成功",message: res.data.message,type: "success"})
                }else{
                    this.$Alert({title: "失败",message: res.data.message,type: "error"})
                }
            })
        },
        showReplaceToolView(){//查看可以更换的托盘
            if(this.selectData.length!=1){
                this.$Alert({title: "失败",message: "请勾选一条需要更换的托盘!",type: "error"})
                return;
            }
            this.toolDrawer.drawer = true;
            request.post("/PointPackageSortRest/queryReplaceTool",this.selectData[0]).then((res)=>{
                this.toolDrawer.tableData = res.data.list;
            })
        },
        replaceToolCode(){//更换托盘
            console.log("xxxxx",this.toolDrawer.currentData);
            if(this.toolDrawer.currentData.length!=1){
                this.$Alert({title: "失败",message: "请勾选一条需要更换的托盘!",type: "error"})
                return;
            }
            var param = {sourceToolCode:this.selectData[0].toolCode,targetToolCode:this.toolDrawer.currentData[0].toolCode};
            request.post("/PointPackageSortRest/replaceToolCode",param).then((res)=>{
                console.log("res",res);
                if(res.data.retCode=="200"){
                    this.$Alert({title: "成功",message: res.data.message,type: "success"})
                }else{
                    this.$Alert({title: "失败",message: res.data.message,type: "error"})
                }
                this.toolDrawer.drawer = false;
                this.$options.methods.doQuery.bind(this)();
            })
        },
        cleanWaitingTask(){
            request.post("/msTaskPackage/cleanWaitingTask",{}).then((res)=>{
                if(res.data.retCode=='200'){
                    this.$Alert({title: "成功",message: res.data.message,type: "success"})
                }else{
                    this.$Alert({ title: "失败",message: res.data.message,type: "error"})
                }
            })
        },
        lookupPackageRule(){
            if(this.selectData.length==0){
                this.$Alert({title: "失败", message: "请至少选择一条数据！",type: "error"});
                return ;
            }
            this.packageRuleDrawer.drawer = true;
            request.post("/materialReceipt/queryPackageTools",this.selectData[0]).then((res)=>{
                this.packageRuleDrawer.tableData = res.data;
            });
        },
        packageTopMatch(){
            if(this.selectData.length!=1){
                this.$Alert({title: "失败", message: "请至少选择一条数据！",type: "error"});
                return ;
            }
            var map = {"i_ms_task_package_id":this.selectData[0].msTaskPackageId,"i_type":"package"}
            request.post("/PointPackageSortRest/packageTopMatch",map).then((res)=>{
                if(res.data.retCode=="200"){
                    this.$Alert({title: "成功", message: res.data.message,type: "success"});
                }else{
                    this.$Alert({title: "失败", message: res.data.message,type: "error"});
                }
            });
        },
        replaceToolByHandle(){
            if(this.selectData.length!=1){
                this.$Alert({ type: 'error', message: '请选择一条数据:'});
                return ;
            }
            this.$prompt('请输入托盘', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消'
            }).then(({ value }) => {
                if(!value.startsWith("21208003")){
                    this.$Alert({ type: 'error', message: '输入的托盘信息不正确:'});
                    retrun ;
                }
                var param = this.selectData[0];
                param.newToolCode = value;
                console.log("param",param);
                request.post("/PointPackageSortRest/replaceToolByHandle",param).then((res)=>{
                    if(res.data.retCode=="200"){
                    this.$Alert({title: "成功",message: res.data.message,type: "success"})
                    }else{
                        this.$Alert({title: "失败",message: res.data.message,type: "error"})
                    }
                    this.toolDrawer.drawer = false;
                    this.$options.methods.doQuery.bind(this)();
                });
            }).catch(() => {});
        }
    }
}
</script>